home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / clang / pgp20src.zip / 8086.ASM < prev    next >
Assembly Source File  |  1992-03-05  |  9KB  |  360 lines

  1. ;  Assembly primitives for RSA multiprecision library
  2. ;  Tested with Turbo Assembler 1.0 and masm 1.00
  3. ;  Written by Branko Lankester (lankeste@fwi.uva.nl)    10/10/91
  4.  
  5. ; define LDATA and LCODE as follows:
  6. ; model:        small   compact medium  large
  7. ; LDATA         0       1       0       1
  8. ; LCODE         0       0       1       1
  9.  
  10. LDATA   equ     1
  11. LCODE   equ     1
  12.  
  13. IF LDATA
  14. DSTPTR  equ     es:[bx+si]
  15. ELSE
  16. DSTPTR  equ     [bx+si]
  17. ENDIF
  18.  
  19. IF LCODE
  20. prec    equ     [bp+6]          ; 1st arg
  21. r1      equ     [bp+6]          ; 1st arg
  22. IF LDATA
  23. r2      equ     [bp+10]         ; 2nd arg
  24. carry   equ     [bp+14]         ; 3rd arg
  25. scarry  equ     [bp+10]         ; carry for shift (arg 2)
  26. ELSE
  27. r2      equ     [bp+8]
  28. carry   equ     [bp+10]
  29. scarry  equ     [bp+8]
  30. ENDIF
  31. ELSE                            ; small code model
  32. prec    equ     [bp+4]
  33. r1      equ     [bp+4]
  34. IF LDATA
  35. r2      equ     [bp+8]
  36. carry   equ     [bp+12]
  37. scarry  equ     [bp+8]
  38. ELSE
  39. r2      equ     [bp+6]
  40. carry   equ     [bp+8]
  41. scarry  equ     [bp+6]
  42. ENDIF
  43. ENDIF
  44.  
  45.  
  46. _TEXT   segment byte public 'CODE'
  47. DGROUP  group   _DATA,_BSS
  48.         assume  cs:_TEXT,ds:DGROUP
  49. _TEXT   ends
  50.  
  51. _DATA   segment word public 'DATA'
  52. _DATA   ends
  53.  
  54. _BSS    segment word public 'BSS'
  55. prec16  dw      ?               ; precision / 16 (seems to be / 256?)
  56. unitprec dw     ?               ; precision / 16, really
  57. addp    dw      ?               ; jump offset
  58. subp    dw      ?
  59. rotp    dw      ?
  60. mulp    dw      ?
  61. _BSS    ends
  62.  
  63. _TEXT   segment byte public 'CODE'
  64.  
  65.         public  _P_SETP
  66.         public  _P_ADDC
  67.         public  _P_SUBB
  68.         public  _P_ROTL
  69.  
  70. IF LCODE
  71. fprims  proc    far                     ; dummy proc
  72. ELSE
  73. fprims  proc    near
  74. ENDIF
  75.  
  76. ;
  77. ; ******************** set precision ********************
  78. ;
  79. _P_SETP:
  80.         push    bp
  81.         mov     bp,sp
  82.         mov     ax, prec        ; precision in bits
  83.         add     ax, 0fh
  84.         mov     cl,4
  85.         shr     ax,cl           ; prec. in units
  86.         mov     unitprec,ax
  87.         push    ax
  88.         shr     ax,cl
  89.         mov     prec16,ax       ; precision / 16
  90.         pop     ax
  91.         and     ax,0fh          ;   al = prec % 16
  92.         mov     bx,ax
  93.         mov     cx,ax
  94.         shl     bx,1            ; multiply by 4 (=number of bytes 
  95.         shl     bx,1            ;   in instruction sequence)
  96.         mov     dx,bx
  97. IFE LDATA
  98.         sub     dx,ax           ; small model only 3 for add/sub
  99. ENDIF
  100.         mov     ax,offset add_ref
  101.         sub     ax,dx
  102.         mov     addp,ax
  103.  
  104.         mov     ax,offset sub_ref
  105.         sub     ax,dx
  106.         mov     subp,ax
  107.  
  108.         mov     ax,offset rot_ref
  109.         sub     ax,bx
  110.         mov     rotp,ax
  111.  
  112.         mov     ax,offset mul_ref
  113.         shl     bx,1            ; MULU macro is 17 bytes for large data
  114.         shl     bx,1
  115.         sub     ax,bx
  116.         sub     ax,cx
  117.         mov     mulp,ax
  118.  
  119.         pop     bp
  120.         ret
  121.  
  122.  
  123.  
  124. ;
  125. ; ******************** mpi add with carry ********************
  126. ;
  127. ADDU    macro   n
  128.         rept    n
  129.                 lodsw
  130.                 adc     DSTPTR,ax
  131.         endm
  132. endm
  133.  
  134.  
  135. _P_ADDC:
  136.         push    bp
  137.         mov     bp,sp
  138.         push    si
  139.         mov     cx, prec16
  140.         mov     dx, addp
  141. IF LDATA
  142.         push    ds
  143.         lds     si, dword ptr r2
  144.         les     bx, dword ptr r1
  145. ELSE
  146.         mov     si, r2
  147.         mov     bx, r1
  148. ENDIF
  149.         sub     bx, si          ; calculate relative offset
  150.         dec     bx
  151.         dec     bx
  152.         cld
  153.         shr     byte ptr carry,1        ; load carry
  154.         jcxz    add_units
  155. add_16u:
  156.         ADDU    16
  157.         loop    add_16u
  158. add_units:
  159.         jmp     dx
  160.         ADDU    15
  161. add_ref:
  162.         rcl     ax,1            ; return carry
  163.         and     ax,1
  164. IF LDATA
  165.         pop     ds
  166. ENDIF
  167.         pop     si
  168.         pop     bp
  169.         ret
  170.  
  171.  
  172.  
  173. ;
  174. ; ******************** mpi subtract with borrow ********************
  175. ;
  176. SUBU    macro   n
  177.         rept    n
  178.                 lodsw
  179.                 sbb     DSTPTR,ax
  180.         endm
  181. endm
  182.  
  183.  
  184. _P_SUBB:
  185.         push    bp
  186.         mov     bp,sp
  187.         push    si
  188.         mov     cx, prec16
  189.         mov     dx, subp
  190. IF LDATA
  191.         push    ds
  192.         lds     si, dword ptr r2
  193.         les     bx, dword ptr r1
  194. ELSE
  195.         mov     si, r2
  196.         mov     bx, r1
  197. ENDIF
  198.         sub     bx, si          ; calculate relative offset
  199.         dec     bx
  200.         dec     bx
  201.         cld
  202.         shr     byte ptr carry,1
  203.         jcxz    sub_units
  204. sub_16u:
  205.         SUBU    16
  206.         loop    sub_16u
  207. sub_units:
  208.         jmp     dx
  209.         SUBU    15
  210. sub_ref:
  211.         rcl     ax,1            ; return carry
  212.         and     ax,1
  213. IF LDATA
  214.         pop     ds
  215. ENDIF
  216.         pop     si
  217.         pop     bp
  218.         ret
  219.  
  220.  
  221.  
  222. ;
  223. ; ******************** mpi rotate left ********************
  224. ;
  225. _P_ROTL:
  226.         push    bp
  227.         mov     bp,sp
  228.         mov     cx, prec16
  229.         mov     dx, rotp
  230. IF LDATA
  231.         push    ds
  232.         lds     bx, dword ptr r1
  233. ELSE
  234.         mov     bx, r1
  235. ENDIF
  236.         shr     byte ptr scarry,1
  237.         jcxz    rot_units
  238. rot_16u:
  239.         i = 0
  240.         rept    16
  241.                 rcl     word ptr [bx + i],1
  242.                 i = i + 2
  243.         endm
  244.         lahf
  245.         add     bx,32
  246.         sahf
  247.         loop    rot_16u
  248. rot_units:
  249.         jmp     dx
  250.         rept    15
  251.                 rcl     word ptr [bx],1
  252.                 inc     bx
  253.                 inc     bx
  254.         endm
  255. rot_ref:
  256.  
  257.         rcl     ax,1
  258.         and     ax,1
  259. IF LDATA
  260.         pop     ds
  261. ENDIF
  262.         pop     bp
  263.         ret
  264.  
  265. fprims  endp
  266.  
  267. _TEXT   ends
  268.  
  269.  
  270.  
  271. ; ***************************************************************
  272. ;  P_SMUL (MULTUNIT *prod, MULTUNIT *multiplicand, MULTUNIT multiplier)
  273. ;       mp_smul routine from Upton's modmult, converted to assembler
  274. ;
  275. ;       Multiply the single-word multiplier times the multiprecision integer 
  276. ;       in multiplicand, accumulating result in prod.  The resulting 
  277. ;       multiprecision prod will be 1 word longer than the multiplicand.   
  278. ;       multiplicand is unit_prec words long.  We add into prod, so caller 
  279. ;       should zero it out first.
  280. ;
  281. ;       NOTE:  Unlike other functions in the multiprecision arithmetic 
  282. ;       library, both multiplicand and prod are pointing at the LSB, 
  283. ;       regardless of byte order of the machine.  On an 80x86, this makes 
  284. ;       no difference.  But if this assembly function is implemented
  285. ;       on a 680x0, it becomes important.
  286. ; ***************************************************************
  287. ;   Variable assignments:
  288. ;       multiplier = [bp+14]
  289. ;       multiplicand = [ds:di]  32-bit pointer
  290. ;       prod = [es:si]          32-bit pointer
  291. ;       unit_prec = cx
  292. ;       p = ax-dx
  293. ;       carry = bx
  294. UPTON_TEXT      SEGMENT  WORD PUBLIC 'CODE'
  295. UPTON_TEXT      ENDS
  296. UPTON_TEXT      SEGMENT
  297.         ASSUME  CS: UPTON_TEXT
  298.         ASSUME  DS: DGROUP
  299.         PUBLIC  _P_SMUL
  300.  
  301. MULU    macro   n
  302.         rept    n
  303.                 lodsw                   ;multiplicand
  304.                 mul     bp              ;multiplier, results (p) to AX/DX
  305.                 add     ax,bx           ;carry
  306.                 adc     dx,0
  307.                 add     ax,WORD PTR es:[di]
  308.                 adc     dx,0
  309.                 mov     bx,dx           ;carry
  310.                 stosw
  311.         endm
  312. endm
  313.  
  314. _P_SMUL PROC FAR
  315.         push    bp
  316.         mov     bp,sp
  317.         push    di
  318.         push    si
  319.         push    ds
  320.         mov     cx,prec16
  321.         mov     ax,mulp
  322.         push    ax
  323.  
  324.         sub     bx,bx           ;carry = 0, store in bx
  325.  
  326.         les     di,DWORD PTR [bp+6]     ;prod in es:di
  327.         lds     si,DWORD PTR [bp+10]    ;multiplicand in ds:si
  328.         cld
  329.         mov     bp,[bp+14]
  330.  
  331.         or      cx,cx
  332.         jnz     mul_16u
  333.         jmp     mul_units
  334. mul_16u:
  335.         MULU    16
  336.         dec     cx
  337.         jz      mul_units
  338.         jmp     mul_16u
  339. mul_units:
  340.         pop     cx
  341.         jmp     cx
  342.         MULU    15
  343. mul_ref:
  344.  
  345.         ; We know that the high-order word of prod will always be 0
  346.         mov     WORD PTR es:[di],bx     ;store carry in prod empty high word
  347.  
  348.         pop     ds
  349.         pop     si
  350.         pop     di
  351.         pop     bp
  352.         ret     
  353.  
  354. _P_SMUL ENDP
  355. UPTON_TEXT   ends
  356.         end
  357.  
  358.